package hikari.script;

import com.gooddies.wiring.annotations.WiringComponent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sad
 */
@WiringComponent(singleton = true, lazy = true)
public class PropertiesExtractor {

    private Pattern pattern = Pattern.compile("//\\[\\[property[ \\t]*\\s[ \\t]*(.*?)[ \\t]+(.*?);[ \\t]*\\s[ \\t]*//property\\]\\]", Pattern.MULTILINE);

    public ScriptProperty[] getScriptProperties(String text) {
        text = text.replace('\r', ' ');
        ArrayList<ScriptProperty> properties = new ArrayList<>();
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            String type = matcher.group(1);
            String name = matcher.group(2);
            ScriptProperty property = new ScriptProperty(type, name);
            properties.add(property);
        }

        return properties.toArray(new ScriptProperty[0]);
    }
}
